import { graphql, useFragment } from 'react-relay';
import React from 'react';
import makeStyles from '@mui/styles/makeStyles';
import Grid from '@mui/material/Grid';
import useHelper from 'src/utils/hooks/useHelper';
import { MalwareAnalysis_malwareAnalysis$key } from './__generated__/MalwareAnalysis_malwareAnalysis.graphql';
import MalwareAnalysisDetails from './MalwareAnalysisDetails';
import StixDomainObjectOverview from '../../common/stix_domain_objects/StixDomainObjectOverview';
import SimpleStixObjectOrStixRelationshipStixCoreRelationships from '../../common/stix_core_relationships/SimpleStixObjectOrStixRelationshipStixCoreRelationships';
import StixCoreObjectExternalReferences from '../external_references/StixCoreObjectExternalReferences';
import StixCoreObjectLatestHistory from '../../common/stix_core_objects/StixCoreObjectLatestHistory';
import StixCoreObjectOrStixCoreRelationshipNotes from '../notes/StixCoreObjectOrStixCoreRelationshipNotes';
import Security from '../../../../utils/Security';
import { KNOWLEDGE_KNUPDATE } from '../../../../utils/hooks/useGranted';
import MalwareAnalysisEdition from './MalwareAnalysisEdition';
import StixCoreObjectOrStixRelationshipLastContainers from '../../common/containers/StixCoreObjectOrStixRelationshipLastContainers';
import useOverviewLayoutCustomization from '../../../../utils/hooks/useOverviewLayoutCustomization';

// Deprecated - https://mui.com/system/styles/basics/
// Do not use it for new code.
const useStyles = makeStyles(() => ({
  gridContainer: {
    marginBottom: 20,
  },
}));

const MalwareAnalysisFragment = graphql`
  fragment MalwareAnalysis_malwareAnalysis on MalwareAnalysis {
    id
    standard_id
    entity_type
    x_opencti_stix_ids
    created
    modified
    created_at
    revoked
    confidence
    product
    submitted
    result_name
    createdBy {
      ... on Identity {
        id
        name
        entity_type
        x_opencti_reliability
      }
    }
    creators {
      id
      name
    }
    objectMarking {
      id
      definition_type
      definition
      x_opencti_order
      x_opencti_color
    }
    objectLabel {
      id
      value
      color
    }
    objectAssignee {
      id
      name
      entity_type
    }
    status {
      id
      order
      template {
        name
        color
      }
    }
    workflowEnabled
    ...MalwareAnalysisDetails_malwareAnalysis
  }
`;

const MalwareAnalysis = ({
  data,
}: {
  data: MalwareAnalysis_malwareAnalysis$key;
}) => {
  const classes = useStyles();
  const { isFeatureEnable } = useHelper();
  const isFABReplaced = isFeatureEnable('FAB_REPLACEMENT');
  const malwareAnalysis = useFragment(MalwareAnalysisFragment, data);
  const linkKnowledge = `/dashboard/analyses/malware_analyses/${malwareAnalysis.id}/knowledge`;
  const overviewLayoutCustomization = useOverviewLayoutCustomization(malwareAnalysis.entity_type);
  return (
    <>
      <Grid
        container={true}
        spacing={3}
        classes={{ container: classes.gridContainer }}
      >
        {
          overviewLayoutCustomization.map(({ key, width }) => {
            switch (key) {
              case 'details':
                return (
                  <Grid key={key} item xs={width}>
                    <MalwareAnalysisDetails
                      malwareAnalysisFragment={malwareAnalysis}
                    />
                  </Grid>
                );
              case 'basicInformation':
                return (
                  <Grid key={key} item xs={width}>
                    <StixDomainObjectOverview
                      stixDomainObject={malwareAnalysis}
                      displayAssignees
                    />
                  </Grid>
                );
              case 'latestCreatedRelationships':
                return (
                  <Grid key={key} item xs={width}>
                    <SimpleStixObjectOrStixRelationshipStixCoreRelationships
                      stixObjectOrStixRelationshipId={malwareAnalysis.id}
                      stixObjectOrStixRelationshipLink={linkKnowledge}
                    />
                  </Grid>
                );
              case 'latestContainers':
                return (
                  <Grid key={key} item xs={width}>
                    <StixCoreObjectOrStixRelationshipLastContainers
                      stixCoreObjectOrStixRelationshipId={malwareAnalysis.id}
                    />
                  </Grid>
                );
              case 'externalReferences':
                return (
                  <Grid key={key} item xs={width}>
                    <StixCoreObjectExternalReferences
                      stixCoreObjectId={malwareAnalysis.id}
                    />
                  </Grid>
                );
              case 'mostRecentHistory':
                return (
                  <Grid key={key} item xs={width}>
                    <StixCoreObjectLatestHistory
                      stixCoreObjectId={malwareAnalysis.id}
                    />
                  </Grid>
                );
              case 'notes':
                return (
                  <Grid key={key} item xs={width}>
                    <StixCoreObjectOrStixCoreRelationshipNotes
                      stixCoreObjectOrStixCoreRelationshipId={malwareAnalysis.id}
                      defaultMarkings={malwareAnalysis.objectMarking ?? []}
                    />
                  </Grid>
                );
              default:
                return null;
            }
          })
        }
      </Grid>
      {!isFABReplaced && (
        <Security needs={[KNOWLEDGE_KNUPDATE]}>
          <MalwareAnalysisEdition malwareAnalysisId={malwareAnalysis.id} />
        </Security>
      )}
    </>
  );
};
export default MalwareAnalysis;
